package org.summer.lawyer.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.summer.common.core.utils.MapstructUtils;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.satoken.utils.LoginHelper;
import org.summer.lawyer.domain.Award;
import org.summer.lawyer.domain.bo.award.AwardBo;
import org.summer.lawyer.domain.vo.award.AwardVo;
import org.summer.lawyer.mapper.AwardMapper;
import org.summer.lawyer.service.IAwardService;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 荣誉信息Service业务层处理
 *
 * @author summer
 * @date 2024-06-25
 */
@RequiredArgsConstructor
@Service
public class AwardServiceImpl extends ServiceImpl<AwardMapper, Award> implements IAwardService {


    @Override
    public TableDataInfo<AwardVo> selectListByScope(AwardVo vo) {
        LambdaQueryWrapper<Award> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(vo.getLawFirmId() != null, Award::getLawFirmId, vo.getLawFirmId())
                .eq(vo.getLawyerId() != null, Award::getLawyerId, vo.getLawyerId())
                .orderByAsc(Award::getAwardId);

        return TableDataInfo.build(baseMapper.selectVoList(queryWrapper));
    }

    /**
     * 查询荣誉信息
     *
     * @param awardId 主键
     * @return 荣誉信息
     */
    @Override
    public AwardVo selectAwardVoById(Long awardId) {
        return baseMapper.selectVoById(awardId);
    }

    /**
     * 分页查询荣誉信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 荣誉信息分页列表
     */
    @Override
    public TableDataInfo<AwardVo> selectPageList(AwardBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Award> lqw = buildQueryWrapper(bo);
        Page<AwardVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的荣誉信息列表
     *
     * @param bo 查询条件
     * @return 荣誉信息列表
     */
    @Override
    public List<AwardVo> selectAwardVoList(AwardBo bo) {
        LambdaQueryWrapper<Award> lqw = Wrappers.lambdaQuery();
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Award> buildQueryWrapper(AwardBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Award> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getAwardScope()), Award::getAwardScope, bo.getAwardScope());
        lqw.eq(StrUtil.isNotBlank(bo.getTitle()), Award::getTitle, bo.getTitle());
        lqw.eq(StrUtil.isNotBlank(bo.getAwardImage()), Award::getAwardImage, bo.getAwardImage());
        lqw.eq(bo.getLawFirmId() != null, Award::getLawFirmId, bo.getLawFirmId());
        lqw.eq(bo.getLawyerId() != null, Award::getLawyerId, bo.getLawyerId());
        lqw.eq(StrUtil.isNotBlank(bo.getAwardContent()), Award::getAwardContent, bo.getAwardContent());
        return lqw;
    }

    /**
     * 新增荣誉信息
     *
     * @param bo 荣誉信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AwardBo bo) {
        Award add = MapstructUtils.convert(bo, Award.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtil.date());
        add.setUpdateTime(DateUtil.date());
        if(LoginHelper.getLoginUser() != null){
            add.setCreateBy(LoginHelper.getUserId());
            add.setUpdateBy(LoginHelper.getUserId());
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAwardId(add.getAwardId());
        }
        return flag;
    }

    /**
     * 修改荣誉信息
     *
     * @param bo 荣誉信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AwardBo bo) {
        Award update = MapstructUtils.convert(bo, Award.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtil.date());
        if(LoginHelper.getLoginUser() != null){
            update.setUpdateBy(LoginHelper.getUserId());
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Award entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除荣誉信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void saveAward(AwardBo bo) {
        if (bo.getAwardId() == null) {
            insertByBo(bo);
        } else {
            updateByBo(bo);
        }
    }
}
